# -*- coding: utf-8 -*-
# @Time    : 2021/10/25 16:46
# @Author  : Wan Fangming


import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from tqdm import tqdm
from collections import Counter

time_list = []
file_path = '../data/query1021.json'
with open(file_path, 'r') as f:
    lines = f.readlines()
    for line in tqdm(lines):
        line = json.loads(line)
        time_list.append(int(line['time']))

count = Counter(time_list)
count = dict(count)
count = sorted(count.items(), key=lambda item: item[1], reverse=True)
item_list = []
num_list = []
for item in count:
    item_list.append(item[0])
    num_list.append(item[1])
data = {'item': item_list, 'num': num_list}
df = pd.DataFrame(data)


def get_stats(group):
    return {'min': group.min(), 'max': group.max(), 'count': group.count(), 'mean': group.mean()}


bins = [0, 20, 50, 150, 350, 650, 2000, 5000]
factor = pd.cut(df['num'], bins=bins)
print(factor)
grouped = df['item'].groupby(factor)
stats = grouped.apply(get_stats).unstack()
x1 = stats["count"].astype(np.int32)
label = ["<20", "20-50", "50-150", "150-350", "350-650", "650-2000", "2000-5000"]
plt.pie(x1, labels=label, autopct="%3.1f%%", startangle=30)

plt.title("time")
plt.show()
